//app.js
var mfApi = require('/api/api.js');
import { bodyLays, addUserStat } from "/pages/utils/testPersonData"
// import testData from "pages/utils/testPersonData.js"
App({
        onLaunch: function () {
                //调用API从本地缓存中获取数据
                var logs = jd.getStorageSync('logs') || [];
                logs.unshift(Date.now());
                jd.setStorageSync('logs', logs);

                this.innerAudioContext = jd.getBackgroundAudioManager();
                // this.innerAudioContext = jd.createInnerAudioContext();

                this.innerAudioContext.title = '此时此刻';
                if (this.globalData.shouldPlaySound) {
                        this.startPlaySound()
                }

                this.userLogin();
                this.saveTrack(' ', 'flowSource');
        },

        userLogin: function () {
                let _self = this;
                console.log('userLogin excute');
                mfApi.http(mfApi.querylogin(), "POST", {
                        "password": "admin123",
                        "username": "admin",
                }).then(res => {
                        mfApi.updateToken(res.token);
                        _self.getMenuData();
                });
        },

        getMenuData: function () {
                let _self = this;
                console.log('userLogin excute');
                mfApi.http(mfApi.getMenuData(), "GET", {
                }).then(res => {
                        console.log(res)

                        this.globalData.gPartsMenuDatas = res.data;
                });
        },

        /**
         * 
         * @param {*} inIsPlay 停止播放
         * @param {*} inType 播放哪个 1 初始  2 换装后
         */
        stopPlaySound: function () {
                // this.innerAudioContext.src = inType == 1 ? type1 : (inType == 2 ? type2 : '');
                this.innerAudioContext.stop();
        },

        startPlaySound() {
                let _self = this;

                let type1 = this.globalData.baseUrl + "/sounds/02%20A3%20VINYL%20TRACK%20MARKER%20AT%204.44-823.mp3";
                let type2 = this.globalData.baseUrl + "/sounds/Long%20Gone%20no%20drums-5682.mp3";

                if (this.globalData.soundIndex == 1)
                        this.innerAudioContext.src = type1;
                else if (this.globalData.soundIndex == 2)
                        this.innerAudioContext.src = type2;

                this.innerAudioContext.onEnded(() => {
                        console.log("startPlaySound   onEnded")
                        _self.startPlaySound();
                });
        },

        changeSoundSource() {
                if (this.globalData.soundIndex == 1)
                        this.globalData.soundIndex = 2;
                else
                        this.globalData.soundIndex = 1;

                if (this.globalData.shouldPlaySound) {
                        this.startPlaySound();
                }
        },

        // 埋点
        saveTrack(usercustomType, isInit = false) {
                let inData = {
                }
                inData.customType = usercustomType;
                inData.version = "v2";
                inData.projectCode = "2189";
                inData.timestamp = new Date().getTime().toString();
                inData.province = "";
                inData.city = "";
                inData.platform = "jd";
                inData.channel = "jd";
                inData.userId = this.globalData.userInfo ? this.globalData.userInfo.openId : "default user";
                if (isInit) {
                        inData.type = "flowSource";
                        delete inData.customType;
                }

                mfApi.http(mfApi.addUserStat(), "POST", inData).then(result => {
                        // console.log("addUserStat  --- ok ")
                        // console.log(result)
                });

                // console.log("saveTrack:" + usercustomType);
        },

        globalData: {
                // 是否播放声音
                shouldPlaySound: true,
                soundIndex: 1,

                userInfo: null,
                baseUrl: "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd",

                // 0 男性；1 女性；2 不告诉你
                selectGender: 1,

                // 0 盲盒；1 空间；2 衣柜
                selectFuncId: "box",

                // 摇一摇剩余次数
                gShakeRemaintimes: 6,

                gShowBodyLays: bodyLays,

                backgroundId: "bg1",
                sceneBackgroundId: 1,

                appSecret: '50c1c1a33cc84a769813608ba3b47285',
                appId: '0ED298335A658F013FA53771DFAF849C',

                // 小构件数据
                gPartsMenuDatas: null,

        }
});